package com.avalon.shop.service;

import com.avalon.core.exception.AvalonException;
import com.avalon.core.model.RecordRow;
import com.avalon.core.util.JacksonUtil;
import com.avalon.core.util.RestTemplateUtils;
import com.avalon.shop.ShopContext;
import com.avalon.shop.config.ShopConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PetService extends BaseService {
    private final ShopContext shopContext;
    private final ShopConfig shopConfig;
    private final String serviceName = "pet.big.type";

    private final String petVaccinationService = "pet.pet.vaccination";
    private final String petDewormService = "pet.pet.deworm";

    public PetService(ShopContext shopContext, ShopConfig shopConfig) {
        super(shopConfig, shopContext);
        this.shopContext = shopContext;
        this.shopConfig = shopConfig;

    }

    public Map getUserPet(Integer petId) {
        return getModelDetail("pet.user.pet",
                "id,name,petCategoryId.id,petCategoryId.name," +
                        "petSmallTypeId.id,petSmallTypeId.name,sex," +
                        "birthday,avatar,state",
                petId);
    }

    public List<Map> getPetCategory() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("fields", "id,name,img,sequence");
        param.put("order", "sequence asc, id desc");
        param.put("serviceName", serviceName);
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(shopConfig.getServer()
                        + "/model/get/all",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            List<Map> maps = JacksonUtil.convert2Object(responseEntity.getBody(), new TypeReference<List<Map>>() {
            });
            return maps;
        } else {
            log.error("getPetCategory 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }

    public List<Map> getPetSmallCategory(Integer bigTypeId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("fields", "id,name");
        param.put("order", "id desc");
        param.put("serviceName", "pet.small.type");
        param.put("rpnCondition", "(=,petBigTypeId," + bigTypeId + ")");
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(shopConfig.getServer()
                        + "/model/get/all",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            List<Map> maps = JacksonUtil.convert2Object(responseEntity.getBody(), new TypeReference<List<Map>>() {
            });
            return maps;
        } else {
            log.error("getPetSmallCategory 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }

    public List<Map> getPetVaccinationList(Integer petId) {
        return getAllModel(petVaccinationService,
                "id,petId,vaccinationName,vaccinationDate",
                "(=,petId," + petId + ")",
                "vaccinationDate desc,id desc");
    }

    public void addPetVaccination(Integer petId, String vaccinationDate, String vaccinationName) {
        Map<String, Object> param = new HashMap<>();
        param.put("petId", petId);
        param.put("vaccinationDate", vaccinationDate);
        param.put("vaccinationName", vaccinationName);
        addModel(petVaccinationService, param);
    }

    /**
     * 获取宠物疫苗名称
     *
     * @param petBigTypeId
     * @return
     */
    public List<Map> getVaccinationAll(Integer petBigTypeId) {
        return getAllModel("pet.vaccination",
                "id,name,petCategoryIds.petBigTypeId",
                "(=,petCategoryIds.petBigTypeId," + petBigTypeId + ")",
                "id desc");
    }

    public List<Map> getDewormList(Integer petId) {
        return getAllModel(petDewormService,
                "id,petId,dewormDate,dewormKind",
                "(=,petId," + petId + ")",
                "dewormDate desc, id desc");
    }

    public void addPetDeworm(Integer petId, String dewormDate, String dewormKind) {
        Map<String, Object> param = new HashMap<>();
        param.put("petId", petId);
        param.put("dewormDate", dewormDate);
        param.put("dewormKind", dewormKind);
        addModel(petDewormService, param);
    }

    @Override
    protected String getServiceName() {
        return serviceName;
    }
}
